package com.rongxun.common.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * 创建者     庄丰泽
 * 创建时间   2019/3/4 14:12
 * 描述
 */

public class FileUtils {
    public static boolean hasSDCard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    public static String getRootFilePath() {
        if (hasSDCard()) {
            // filePath:/sdcard/
            return Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
        } else {
            // filePath: /data/data/
            return Environment.getDataDirectory().getAbsolutePath() + "/data/";
        }
    }

    public static void deleteFile(File file) {
        if (file.exists()) {
            file.delete();
        }
    }

    public static void createDirectory(String pdkDir) {
        File file = new File(pdkDir);

        if (!file.exists()) {
            file.mkdir();
        }
    }


    /**
     * 判断公有目录文件是否存在，适配安卓10
     */
    public static boolean isAndroidQFileExists(Context context, String path) {
        AssetFileDescriptor afd = null;
        ContentResolver cr = context.getContentResolver();

        Uri uri = Uri.parse(path);
        try {
            afd = cr.openAssetFileDescriptor(uri, "r");
            if (afd != null) {
                return true;
            }
        } catch (FileNotFoundException e) {
            return false;
        }
        return false;
    }
}
